package com.cea.papyrus.core.commands.template;

import org.eclipse.uml2.uml.ParameterableElement;
import org.eclipse.uml2.uml.TemplateBinding;
import org.eclipse.uml2.uml.TemplateParameter;
import org.eclipse.uml2.uml.TemplateParameterSubstitution;
import org.eclipse.uml2.uml.TemplateableElement;

import com.cea.papyrus.core.commands.PapyrusCommand;

public class TemplateParameterSubstitutionCreateCommand extends PapyrusCommand {

//	public final static int PARAMETERED_ELEMENT = 0 ;
//	public final static int DEFAULT = 1 ;
//	public final static int CONSTRAINING_CLASSIFIER = 2 ;
	
	/**
	 * parent of the created element.
	 */
	private TemplateBinding umlParentTemplateBinding ;
	
	/**
	 * parent of the created element.
	 */
	private TemplateParameter formalParameter ;
	
	/**
	 * parent of the created element.
	 */
	private TemplateableElement actualParameter ;
	
	/**
	 * parent of the created element.
	 */
	private TemplateParameterSubstitution createdParameterSubstitution ;
	
	
	public TemplateParameterSubstitutionCreateCommand(Class elementClass, TemplateBinding umlParent, TemplateParameter formal, TemplateableElement actual) {
//		super(elementClass) ; // this statement is useless
		this.umlParentTemplateBinding = umlParent ;
		this.formalParameter = formal ;
		this.actualParameter = actual ;
		this.createdParameterSubstitution = null ;
		this.setLabel("Create parameter substitution") ;
	}
	
	public void execute() {
		createdParameterSubstitution = umlParentTemplateBinding.createParameterSubstitution() ;
		createdParameterSubstitution.setFormal(formalParameter) ;
		if (actualParameter != null)
			createdParameterSubstitution.getActuals().add((ParameterableElement)actualParameter) ;
	}
	
	public boolean canExecute() {
		return umlParentTemplateBinding instanceof TemplateBinding ; 
	}
	
	public void redo() {
		execute() ;
	}
	
	public void undo() {
		createdParameterSubstitution.setFormal(null) ;
		if (actualParameter != null) {
			createdParameterSubstitution.getActuals().remove(actualParameter) ;
		}
		umlParentTemplateBinding.getParameterSubstitutions().remove(createdParameterSubstitution) ;
	}
	
	public boolean canUndo() {
		return createdParameterSubstitution != null ;
	}
	
	public boolean canRedo() {
		return createdParameterSubstitution != null ;
	}
}
